package org.platform.processor.knowledge;

import lombok.extern.slf4j.Slf4j;
import org.platform.constant.StationConst;
import org.platform.dao.mapper.*;
import org.platform.mo.bussmo.diagnose.event.AlarmBO;
import org.platform.mo.csmo.common.KB_Main;
import org.platform.mo.csmo.common.KB_Monitor;
import org.platform.mo.csmo.common.KnowMonitorApplyBean;
import org.platform.mo.db.*;
import org.platform.tool.helper.ResourceHelper;
import org.platform.tool.util.BeansUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 故障检测点 检测组
 *
 * @author FengJie
 * @date 2018/4/20
 */
@Service
@Slf4j
public class EventProcessor {
    @Autowired
    private FdKbEventMapper eventMapper;
    @Autowired
    private FdKashiEventMapper kashiEventMapper;
    @Autowired
    private FdMiyunEventMapper miyunEventMapper;
    @Autowired
    private FdSanyaEventMapper sanyaEventMapper;
    @Autowired
    private FdXinanEventMapper xinanEventMapper;
    @Autowired
    private ResourceHelper resource;


    /**
     * 根据知识主表,获取对应的故障监测信息
     *
     * @param main
     * @return
     */
    public KnowMonitorApplyBean getKnowMonitorByKBMain(KB_Main main) {
        KnowMonitorApplyBean applyBean = new KnowMonitorApplyBean();
        Example example = new Example(FdKbEvent.class);
        example.createCriteria()
                .andEqualTo("knowledgeid", main.getKnowledgeid())
                .andEqualTo("station", main.getStation())
                .andEqualTo("version", main.getVersion());

        List<FdKbEvent> eventList = eventMapper.selectByExample(example);
        List<KB_Monitor> monitorList = BeansUtils.copyListProperties(eventList, KB_Monitor.class);
        applyBean.setKb_monitorList(monitorList);

        return applyBean;
    }

    /**
     * 插入的故障监测信息
     *
     * @param applyBean
     */
    public void addKnowMonitor(KnowMonitorApplyBean applyBean) {
        List<KB_Monitor> monitorList = applyBean.getKb_monitorList();
        List<FdKbEvent> eventList = BeansUtils.copyListProperties(monitorList, FdKbEvent.class);
        eventList.forEach(event -> eventMapper.insert(event));
    }

    /**
     * 更新当前站点的设备状态
     *
     * @param alarmBO
     */
    public void addOrModifyEventByStation(AlarmBO alarmBO) {
        //当前站点
        String stationName = resource.getStationName();
        switch (stationName) {
            case StationConst.XINAN_STATION:
                FdXinanEvent xinanEvent = new FdXinanEvent();
                BeanUtils.copyProperties(alarmBO, xinanEvent);
                if (0 == alarmBO.getIsrestored()) {
                    if (null == xinanEventMapper.selectByPrimaryKey(xinanEvent.getId())) {
                        xinanEventMapper.insert(xinanEvent);
                    }
                } else {
                    Example example = new Example(FdXinanEvent.class);
                    String id = xinanEvent.getDeviceid() + "_" + xinanEvent.getDevattr();
                    example.createCriteria().andLike("id", id + "%");
                    xinanEventMapper.updateByExampleSelective(xinanEvent, example);
                }
                break;

            case StationConst.KASHA_STATION:
                FdKashiEvent kashiEvent = new FdKashiEvent();
                BeanUtils.copyProperties(alarmBO, kashiEvent);
                if (0 == alarmBO.getIsrestored()) {
                    if (null == kashiEventMapper.selectByPrimaryKey(kashiEvent.getId())) {
                        kashiEventMapper.insert(kashiEvent);
                    }
                } else {
                    Example example = new Example(FdKashiEvent.class);
                    String id = kashiEvent.getDeviceid() + "_" + kashiEvent.getDevattr();
                    example.createCriteria().andLike("id", id + "%");
                    kashiEventMapper.updateByExampleSelective(kashiEvent, example);
                }
                break;

            case StationConst.MIYUN_STATION:
                FdMiyunEvent miyunEvent = new FdMiyunEvent();
                BeanUtils.copyProperties(alarmBO, miyunEvent);
                if (0 == alarmBO.getIsrestored()) {
                    if (null == miyunEventMapper.selectByPrimaryKey(miyunEvent.getId())) {
                        miyunEventMapper.insert(miyunEvent);

                    }
                } else {
                    Example example = new Example(FdMiyunEvent.class);
                    String id = miyunEvent.getDeviceid() + "_" + miyunEvent.getDevattr();
                    example.createCriteria().andLike("id", id + "%");
                    miyunEventMapper.updateByExampleSelective(miyunEvent, example);
                }
                break;

            case StationConst.SANYA_STATION:
                FdSanyaEvent sanyaEvent = new FdSanyaEvent();
                BeanUtils.copyProperties(alarmBO, sanyaEvent);
                if (0 == alarmBO.getIsrestored()) {
                    if (null == sanyaEventMapper.selectByPrimaryKey(sanyaEvent.getId())) {
                        sanyaEventMapper.insert(sanyaEvent);
                    }
                } else {
                    Example example = new Example(FdSanyaEvent.class);
                    String id = sanyaEvent.getDeviceid() + "_" + sanyaEvent.getDevattr();
                    example.createCriteria().andLike("id", id + "%");
                    sanyaEventMapper.updateByExampleSelective(sanyaEvent, example);
                }
                break;

            default:
                log.info("无效的数据!!!");

        }
    }

}
